package com.myspring.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.Page;
import com.myspring.entity.*;
import com.myspring.mapper.ArticleMapper;
import com.myspring.mapper.CategoryMapper;
import com.myspring.mapper.UserMapper;
import com.myspring.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class MyServiceImpl implements MyService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CategoryMapper categoryMapper;


    @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
    @Override
    public List<Article> queryArticleByUid(Integer uid) throws Exception{
        ArticleExample articleExample = new ArticleExample();
        articleExample.createCriteria().andUidEqualTo(uid);
        return this.articleMapper.selectByExample(articleExample);
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
    @Override
    public User checkUser(String username,String password) throws Exception{
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(username).andPasswordEqualTo(password);
        if (userMapper.selectByExample(userExample).isEmpty()){
            User user1 = new User();
            user1.setId(0);
            return user1;
        }
        return userMapper.selectByExample(userExample).get(0);
    }


    @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
    @Override
    public List<Article> queryArticleById(Integer id) throws Exception{
        ArticleExample articleExample2=new ArticleExample();
        articleExample2.createCriteria().andIdEqualTo(id);
        return this.articleMapper.selectByExample(articleExample2);

    }
    @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
    @Override
    public Article queryArticleById2(Integer id) throws Exception{
        ArticleExample articleExample3=new ArticleExample();
        articleExample3.createCriteria().andIdEqualTo(id);
        return this.articleMapper.selectByExample(articleExample3).get(0);

    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
    @Override
    public Boolean deleteById(Integer id) throws  Exception{
        this.articleMapper.deleteByPrimaryKey(id);
        return true;

    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
    @Override
    public List<Category> queryAllCate() throws  Exception{

        return this.categoryMapper.selectByExample(null);

    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    @Override
    public Boolean addArticle(Integer uid, Date edittime, String mdcontent, String title) throws  Exception{

    Article article=new Article();
    article.setUid(uid);
    article.setEdittime(edittime);
    article.setMdcontent(mdcontent);
    article.setTitle(title);
    this.articleMapper.insert(article);
    return true;
    }


    @Override
    public Boolean updatePassword(Integer uid,String password) throws  Exception{
        UserExample userExample=new UserExample();
        userExample.createCriteria().andIdEqualTo(uid);
       List<User> u= this.userMapper.selectByExample(userExample);
        User u1= u.get(0);
         u1.setPassword(password);
         this.userMapper.updateByPrimaryKey(u1);
        return true;
    }
             //分页助手
    @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
    @Override
    public Page<User> queryAllUser(Integer pageNum) throws  Exception{
        PageHelper.startPage(pageNum,3);
        Page<User> page=(Page<User>)this.userMapper.selectByExample(null);

        return  page;
    }

    @Override
    public  Boolean updateArticle( Integer id,String mdcontent) throws  Exception{
                  ArticleExample articleExample=new ArticleExample();
                  articleExample.createCriteria().andIdEqualTo(id);
                  List<Article> a=this.articleMapper.selectByExample(articleExample);
                  Article a1=a.get(0);
                  a1.setMdcontent(mdcontent);
                  this.articleMapper.updateByPrimaryKey(a1);
                  return true;
    }

}
